home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / DJGPP / LGP250S2.ZIP / src / libgplus.5 / libio / gen-para < prev    next >
Text File  |  1993-10-07  |  14KB  |  445 lines

  1. #!/bin/sh
  2. # Copyright (C) 1992, 1993 Free Software Foundation
  3. # This file is part of the GNU IO Library.  This library is free
  4. # software; you can redistribute it and/or modify it under the
  5. # terms of the GNU General Public License as published by the
  6. # Free Software Foundation; either version 2, or (at your option)
  7. # any later version.
  8. # This library is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11. # GNU General Public License for more details.
  12. # You should have received a copy of the GNU General Public License
  13. # along with GNU CC; see the file COPYING.  If not, write to
  14. # the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  15.  
  16. #    Written by Per Bothner (bothner@cygnus.com)
  17.  
  18. # This is a shell-script that figures out various things about a
  19. # system, and writes (to stdout) a C-style include files with
  20. # suitable definitions, including all the standard Posix types.
  21. # It works by compiling various test programs -- some are run through
  22. # the C pre-processor, and the output examined.
  23. # The test programs are only compiled, not executed, so the script
  24. # should even if you're cross-compiling.
  25. # It uses $CC (which defaults to cc) to compile C programs (extension .c),
  26. # and $CXX (which defaults to gcc) to compile C++ programs (extension .C).
  27. # The shell-script is written for libg++.a.
  28.  
  29. # Usage: gen-params [NAME1=name1 ...]
  30. # - where an assignment (such as size_t="unsigned int" means to
  31. # use that value, instead of trying to figure it out.
  32.  
  33. # Uncomment following line for debugging
  34. # set -x
  35.  
  36. SED=sed
  37.  
  38. # Evaluate the arguments (which should be assignments):
  39. for arg in "$@"; do
  40.   # Quote arg (i.e. FOO=bar => FOO='bar'), then eval it.
  41.   eval `echo "$arg" | ${SED} -e "s|^\(.*\)=\(.*\)|\1='\2'|"`
  42. done
  43.  
  44. macro_prefix=${macro_prefix-"_G_"}
  45. rootdir=`pwd`/..
  46. gccdir=${gccdir-${rootdir}/gcc}
  47. binutilsdir=${binutilsdir-${rootdir}/binutils}
  48. CC=${CC-`if [ -f ${gccdir}/xgcc ] ; \
  49.     then echo ${gccdir}/xgcc -B${gccdir}/ -I${gccdir}/include ; \
  50.     else echo cc ; fi`}
  51. CXX=${CXX-`if [ -f ${gccdir}/xgcc ] ; \
  52.     then echo ${gccdir}/xgcc -B${gccdir}/ -I${gccdir}/include ; \
  53.     else echo gcc ; fi`}
  54. CPP=${CPP-`echo ${CC} -E`}
  55. CONFIG_NM=${CONFIG_NM-`if [ -f ${binutilsdir}/nm ] ; \
  56.     then echo ${binutilsdir}/nm ; \
  57.     else echo nm ; fi`}
  58.  
  59. cat <<!EOF!
  60. /* AUTOMATICALLY GENERATED; DO NOT EDIT! */ 
  61. #ifndef ${macro_prefix}config_h
  62. #define ${macro_prefix}config_h
  63. !EOF!
  64.  
  65. if [ x"${LIB_VERSION}" != "x" ] ; then
  66.   echo "#define ${macro_prefix}LIB_VERSION" '"'${LIB_VERSION}'"'
  67. fi
  68.  
  69. # This program is used to test if the compiler prepends '_' before identifiers.
  70. # It is also used to check the g++ uses '$' or '.' various places.
  71.  
  72. if test -z "${NAMES_HAVE_UNDERSCORE}" -o -z "${DOLLAR_IN_LABEL}" ; then
  73.   cat >dummy.C <<!EOF!
  74.   struct filebuf {
  75.       virtual int foo();
  76.   };
  77.   filebuf ff;
  78.   extern "C" int FUNC(int);
  79.   int FUNC(int i) { return i+10; }
  80. !EOF!
  81.  
  82.   if ${CXX} -c dummy.C ; then
  83.     if test -n "${NAMES_HAVE_UNDERSCORE}" ; then
  84.      echo "#define ${macro_prefix}NAMES_HAVE_UNDERSCORE ${NAMES_HAVE_UNDERSCORE}"
  85.     elif test "`${CONFIG_NM} dummy.o | grep _FUNC`" != ""; then
  86.       echo "#define ${macro_prefix}NAMES_HAVE_UNDERSCORE 1"
  87.     elif test "`${CONFIG_NM} dummy.o | grep FUNC`" != ""; then
  88.       echo "#define ${macro_prefix}NAMES_HAVE_UNDERSCORE 0"
  89.     else
  90.       echo "${CONFIG_NM} failed to find FUNC in dummy.o!" 1>&2; exit -1;
  91.     fi
  92.  
  93. #    if test -n "${DOLLAR_IN_LABEL}" ; then
  94. #      echo "#define ${macro_prefix}DOLLAR_IN_LABEL ${DOLLAR_IN_LABEL}"
  95. #    elif test "`${CONFIG_NM} dummy.o | grep 'vt[$$]7filebuf'`" != ""; then
  96. #      echo "#define ${macro_prefix}DOLLAR_IN_LABEL 1"
  97. #    elif test "`${CONFIG_NM} dummy.o | grep 'vt[.]7filebuf'`" != ""; then
  98. #      echo "#define ${macro_prefix}DOLLAR_IN_LABEL 0"
  99. #    elif test "`${CONFIG_NM} dummy.o | grep 'vtbl__7filebuf'`" != ""; then
  100. #      echo "#define ${macro_prefix}DOLLAR_IN_LABEL 0"
  101. #    else
  102. #      echo "gen-params: ${CONFIG_NM} failed to find vt[.\$]filebuf in dummy.o!" 1>&2; exit 1
  103. #    fi
  104.   else
  105.     # The compile failed for some reason (no C++?)
  106.     echo "gen-params: could not compile dummy.C with ${CXX}" 1>&2; exit 1;
  107.   fi
  108. fi
  109.  
  110. # A little test program to check if struct stat has st_blksize.
  111. cat >dummy.c <<!EOF!
  112. #include <sys/types.h>
  113. #include <sys/stat.h>
  114. int BLKSIZE(struct stat *st)
  115. {
  116.     return st->st_blksize;
  117. }
  118. !EOF!
  119.  
  120. if ${CC} -c dummy.c >/dev/null 2>&1 ; then
  121.   echo "#define ${macro_prefix}HAVE_ST_BLKSIZE 1"
  122. else
  123.   echo "#define ${macro_prefix}HAVE_ST_BLKSIZE 0"
  124. fi
  125.  
  126. # Next, generate definitions for the standard types (such as mode_t)
  127. # compatible with those in the standard C header files.
  128. # It works by a dummy program through the C pre-processor, and then
  129. # using sed to search for typedefs in the output.
  130.  
  131. cat >dummy.c <<!EOF!
  132. #include <sys/types.h>
  133. #include <stddef.h>
  134. #include <stdarg.h>
  135. #include <stdio.h>
  136. #include <time.h>
  137. #include <signal.h>
  138. #ifdef size_t
  139. typedef size_t Xsize_t;
  140. #elif defined(__SIZE_TYPE__)
  141. typedef __SIZE_TYPE__ Xsize_t;
  142. #endif
  143. #ifdef ptrdiff_t
  144. typedef ptrdiff_t Xptrdiff_t;
  145. #elif defined(__PTRDIFF_TYPE__)
  146. typedef __PTRDIFF_TYPE__ Xptrdiff_t;
  147. #endif
  148. #ifdef wchar_t
  149. typedef wchar_t Xwchar_t;
  150. #elif defined(__WCHAR_TYPE__)
  151. typedef __WCHAR_TYPE__ Xwchar_t;
  152. #endif
  153. #ifdef va_list
  154. typedef va_list XXXva_list;
  155. #endif
  156. #ifdef BUFSIZ
  157. int XBUFSIZ=BUFSIZ;
  158. #endif
  159. #ifdef FOPEN_MAX
  160. int XFOPEN_MAX=FOPEN_MAX;
  161. #endif
  162. #ifdef FILENAME_MAX
  163. int XFILENAME_MAX=FILENAME_MAX;
  164. #endif
  165. !EOF!
  166.  
  167. if ${CPP} dummy.c >TMP ; then true
  168. else
  169.   echo "gen-params: could not invoke ${CPP} on dummy.c" 1>&2 ; exit 1
  170. fi
  171. tr '    ' ' ' <TMP >dummy.out
  172.  
  173. for TYPE in dev_t clock_t fpos_t gid_t ino_t mode_t nlink_t off_t pid_t ptrdiff_t sigset_t size_t ssize_t time_t uid_t va_list wchar_t int32_t uint_32_t ; do
  174.     IMPORTED=`eval 'echo $'"$TYPE"`
  175.     if [ -n "${IMPORTED}" ] ; then
  176.     eval "$TYPE='$IMPORTED"
  177.     else
  178.     # Search dummy.out for a typedef for $TYPE, and write it out
  179.     # to TMP in #define syntax.
  180.     rm -f TMP
  181.     ${SED} -n -e "s|typedef  *\(.*\) X*$TYPE *;|\1|w TMP" <dummy.out>/dev/null
  182.     # Now select the first definition.
  183.         if [ -s TMP ]; then
  184.         # VALUE is now the typedef'd definition of $TYPE.
  185.             eval "VALUE='`${SED} -e 's| *$||' -e '2,$d' <TMP`'"
  186.         # Unless VALUE contains a blank, look for a typedef for it
  187.         # in turn (this could be a loop, but that would be over-kill).
  188.         if echo $VALUE | grep " " >/dev/null ; then true
  189.         else
  190.         rm -f TMP
  191.         ${SED} -n -e "s|typedef[     ][     ]*\(.*[^a-zA-Z0-9_]\)${VALUE}[     ]*;.*|\1|w TMP" <dummy.out>/dev/null
  192.         if [ -s TMP ]; then
  193.             eval "VALUE='`${SED} -e '2,$d' -e 's|[     ]*$||' <TMP`'"
  194.         fi
  195.         fi
  196.         eval "$TYPE='$VALUE'"
  197.     fi
  198.     fi
  199. done
  200.  
  201. cat <<!EOF!
  202. typedef ${clock_t-int /* default */} ${macro_prefix}clock_t;
  203. typedef ${dev_t-int /* default */} ${macro_prefix}dev_t;
  204. typedef ${fpos_t-long /* default */} ${macro_prefix}fpos_t;
  205. typedef ${gid_t-int /* default */} ${macro_prefix}gid_t;
  206. typedef ${ino_t-int /* default */} ${macro_prefix}ino_t;
  207. typedef ${mode_t-int /* default */} ${macro_prefix}mode_t;
  208. typedef ${nlink_t-int /* default */} ${macro_prefix}nlink_t;
  209. typedef ${off_t-long /* default */} ${macro_prefix}off_t;
  210. typedef ${pid_t-int /* default */} ${macro_prefix}pid_t;
  211. typedef ${ptrdiff_t-long int /* default */} ${macro_prefix}ptrdiff_t;
  212. typedef ${sigset_t-int /* default */} ${macro_prefix}sigset_t;
  213. typedef ${size_t-unsigned long /* default */} ${macro_prefix}size_t;
  214. typedef ${time_t-int /* default */} ${macro_prefix}time_t;
  215. typedef ${uid_t-int /* default */} ${macro_prefix}uid_t;
  216. typedef ${wchar_t-int /* default */} ${macro_prefix}wchar_t;
  217. typedef ${int32_t-int /* default */} ${macro_prefix}int32_t;
  218. typedef ${uint32_t-unsigned int /* default */} ${macro_prefix}uint32_t;
  219. !EOF!
  220.  
  221.  
  222. # ssize_t is the signed version of size_t
  223. if [ -n "${ssize_t}" ] ; then
  224.     echo "typedef ${ssize_t} ${macro_prefix}ssize_t;"
  225. elif [ -z "${size_t}" ] ; then
  226.     echo "typedef long ${macro_prefix}ssize_t;"
  227. else
  228.     # Remove "unsigned" from ${size_t} to get ${ssize_t}.
  229.     tmp="`echo ${size_t} | ${SED} -e 's|unsigned||g' -e 's|  | |g'`"
  230.     if [ -z "$tmp" ] ; then
  231.     tmp=int
  232.     else
  233.     # check $tmp doesn't conflict with <unistd.h>
  234.     echo "#include <unistd.h>
  235.     extern $tmp read();" >dummy.c
  236.     ${CC} -c dummy.c >/dev/null 2>&1 || tmp=int
  237.     fi
  238.     echo "typedef $tmp /* default */ ${macro_prefix}ssize_t;"
  239. fi
  240.  
  241. # va_list can cause problems (e.g. some systems have va_list as a struct).
  242. # Check to see if ${va_list-char*} really is compatible with stdarg.h.
  243. cat >dummy.C <<!EOF!
  244. #define X_va_list ${va_list-char* /* default */}
  245. extern long foo(X_va_list ap); /* Check that X_va_list compiles on its own */
  246. extern "C" {
  247. #include <stdarg.h>
  248. }
  249. long foo(X_va_list ap) { return va_arg(ap, long); }
  250. long bar(int i, ...)
  251. { va_list ap; long j; va_start(ap, i); j = foo(ap); va_end(ap); return j; }
  252. !EOF!
  253. if ${CXX} -c dummy.C >/dev/null 2>&1 ; then
  254.   # Ok: We have something that works.
  255.   echo "typedef ${va_list-char* /* default */} ${macro_prefix}va_list;"
  256. else
  257.   # No, it breaks.  Indicate that <stdarg.h> must be included.
  258.   echo "#define ${macro_prefix}NEED_STDARG_H
  259. #define ${macro_prefix}va_list va_list"
  260. fi
  261.  
  262. cat >dummy.c <<!EOF!
  263. #include <signal.h>
  264. extern int (*signal())();
  265. extern int dummy (int);
  266. main()
  267. {
  268.     int (*oldsig)(int) = signal (1, dummy);
  269.     (void) signal (2, oldsig);
  270.     return 0;
  271. }
  272. !EOF!
  273. if ${CC} -c dummy.c >/dev/null 2>&1 ; then
  274.   echo "#define ${macro_prefix}signal_return_type int"
  275. else
  276.   echo "#define ${macro_prefix}signal_return_type void"
  277. fi
  278.  
  279. # check sprintf return type
  280.  
  281. cat >dummy.c <<!EOF!
  282. #include <stdio.h>
  283. extern int sprintf(); char buf[100];
  284. int main() { return sprintf(buf, "%d", 34); }
  285. !EOF!
  286. if ${CC} -c dummy.c >/dev/null 2>&1 ; then
  287.   echo "#define ${macro_prefix}sprintf_return_type int"
  288. else
  289.   echo "#define ${macro_prefix}sprintf_return_type char*"
  290. fi
  291.  
  292. # Look for some standard macros
  293. for NAME in BUFSIZ FOPEN_MAX FILENAME_MAX NULL ; do
  294.     IMPORTED=`eval 'echo $'"$NAME"`
  295.     if [ -n "${IMPORTED}" ] ; then
  296.     eval "$NAME='$IMPORTED /* specified */"
  297.     else
  298.     rm -f TMP
  299.     ${SED} -n -e "s|int X${NAME}=\(.*\);|\1|w TMP" <dummy.out>/dev/null
  300.     # Now select the first definition.
  301.     if [ -s TMP ]; then
  302.         eval "$NAME='"`${SED} -e '2,$d' <TMP`"'"
  303.     fi
  304.     fi
  305. done
  306.  
  307. cat <<!EOF!
  308. #define ${macro_prefix}BUFSIZ ${BUFSIZ-1024 /* default */}
  309. #define ${macro_prefix}FOPEN_MAX ${FOPEN_MAX-32 /* default */}
  310. #define ${macro_prefix}FILENAME_MAX ${FILENAME_MAX-1024 /* default */}
  311. #define ${macro_prefix}NULL ${NULL-0 /* default */}
  312. #define ${macro_prefix}ARGS(ARGLIST) (...)
  313. !EOF!
  314.  
  315. rm -f dummy.c dummy.o
  316.  
  317. if test -n "${HAVE_ATEXIT}" ; then
  318.  echo "#define ${macro_prefix}HAVE_ATEXIT ${HAVE_ATEXIT}"
  319. else
  320.   cat >dummy.c <<!EOF!
  321. #include <stdlib.h>
  322. int main()
  323. {
  324.   atexit (0);
  325. }
  326. !EOF!
  327.   if ${CC} dummy.c >/dev/null 2>&1 ; then
  328.     echo "#define ${macro_prefix}HAVE_ATEXIT 1"
  329.   else
  330.     echo "#define ${macro_prefix}HAVE_ATEXIT 0"
  331.   fi
  332. fi
  333.  
  334.  
  335. # *** Check for presence of certain include files ***
  336.  
  337. # check for sys/resource.h
  338.  
  339. if test -n "${HAVE_SYS_RESOURCE}" ; then
  340.  echo "#define ${macro_prefix}HAVE_SYS_RESOURCE ${HAVE_SYS_RESOURCE}"
  341. else
  342.   cat >dummy.c <<!EOF!
  343. #include <sys/time.h>
  344. #include <sys/resource.h>
  345.   int main()
  346.   {
  347.     struct rusage res;
  348.     getrusage(RUSAGE_SELF, &res);
  349.     return (int)(res.ru_utime.tv_sec + (res.ru_utime.tv_usec / 1000000.0));
  350.   }
  351. !EOF!
  352.   # Note: We link because some systems have sys/resource, but not getrusage().
  353.   if ${CC} dummy.c >/dev/null 2>&1 ; then
  354.     echo "#define ${macro_prefix}HAVE_SYS_RESOURCE 1"
  355.   else
  356.     echo "#define ${macro_prefix}HAVE_SYS_RESOURCE 0"
  357.   fi
  358. fi
  359.  
  360. # check for sys/socket.h
  361.  
  362. if test -n "${HAVE_SYS_SOCKET}" ; then
  363.  echo "#define ${macro_prefix}HAVE_SYS_SOCKET ${HAVE_SYS_SOCKET}"
  364. else
  365.   echo '#include <sys/types.h>' >dummy.c
  366.   echo '#include <sys/socket.h>' >>dummy.c
  367.   if ${CC} -c dummy.c >/dev/null 2>&1 ; then
  368.     echo "#define ${macro_prefix}HAVE_SYS_SOCKET 1"
  369.   else
  370.     echo "#define ${macro_prefix}HAVE_SYS_SOCKET 0"
  371.   fi
  372. fi
  373.  
  374. # Check for a (Posix-compatible) sys/wait.h */
  375.  
  376. if test -n "${HAVE_SYS_WAIT}" ; then
  377.  echo "#define ${macro_prefix}HAVE_SYS_WAIT ${HAVE_SYS_WAIT}"
  378. else
  379.   cat >dummy.C <<!EOF!
  380.   extern "C" {
  381. #include <sys/types.h>
  382. #include <sys/wait.h>
  383.   }
  384.   int f() { int i; wait(&i); return i; }
  385. !EOF!
  386. #define ${macro_prefix}HAVE_UNION_WAIT 1"
  387.   if ${CXX} -c dummy.C >/dev/null 2>&1 ; then
  388.     echo "#define ${macro_prefix}HAVE_SYS_WAIT 1"
  389.   else
  390.     echo "#define ${macro_prefix}HAVE_SYS_WAIT 0"
  391.   fi
  392. fi
  393.  
  394. if test -n "${HAVE_UNISTD}" ; then
  395.  echo "#define ${macro_prefix}HAVE_UNISTD ${HAVE_UNISTD}"
  396. else
  397.   echo '#include <unistd.h>' >dummy.c
  398.   if ${CC} -c dummy.c >/dev/null 2>&1 ; then
  399.     echo "#define ${macro_prefix}HAVE_UNISTD 1"
  400.   else
  401.     echo "#define ${macro_prefix}HAVE_UNISTD 0"
  402.   fi
  403. fi
  404.  
  405. if test -n "${HAVE_DIRENT}" ; then
  406.  echo "#define ${macro_prefix}HAVE_DIRENT ${HAVE_DIRENT}"
  407. else
  408.   echo '#include <sys/types.h>
  409. #include <dirent.h>' >dummy.c
  410.   if ${CC} -c dummy.c >/dev/null 2>&1 ; then
  411.     echo "#define ${macro_prefix}HAVE_DIRENT 1"
  412.   else
  413.     echo "#define ${macro_prefix}HAVE_DIRENT 0"
  414.   fi
  415. fi
  416.  
  417. if test -n "${HAVE_CURSES}" ; then
  418.  echo "#define ${macro_prefix}HAVE_CURSES ${HAVE_CURSES}"
  419. else
  420.   echo '#include <curses.h>' >dummy.c
  421.   if ${CC} -c dummy.c >/dev/null 2>&1 ; then
  422.     echo "#define ${macro_prefix}HAVE_CURSES 1"
  423.   else
  424.     echo "#define ${macro_prefix}HAVE_CURSES 0"
  425.   fi
  426. fi
  427.  
  428. # There is no test for this at the moment; it is just set by the
  429. # configuration files.
  430. if test -n "${MATH_H_INLINES}" ; then
  431.   echo "#define ${macro_prefix}MATH_H_INLINES ${MATH_H_INLINES}"
  432. else
  433.   echo "#define ${macro_prefix}MATH_H_INLINES 0"
  434. fi
  435.  
  436. # Uncomment the following line if you don't have working templates.
  437. # echo "#define ${macro_prefix}NO_TEMPLATES"
  438.  
  439. rm -f dummy.C dummy.o dummy.c dummy.out TMP core a.out
  440.  
  441. echo "#endif /* !${macro_prefix}config_h */"
  442.